package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.common.Result;
import com.amsp.oa.oademo.entity.ArchiveBorrow;
import com.amsp.oa.oademo.service.IArchiveBorrowService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "档案借阅管理")
@RestController
@RequestMapping("/api/archive-borrow")
public class ArchiveBorrowController {

    @Autowired
    private IArchiveBorrowService archiveBorrowService;

    @ApiOperation("分页查询档案借阅记录")
    @GetMapping("/page")
    public Result<IPage<ArchiveBorrow>> getPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("被申请人姓名") @RequestParam(required = false) String applicantName,
            @ApiParam("被申请人工号") @RequestParam(required = false) String applicantEmployeeNumber) {

        IPage<ArchiveBorrow> pageResult = archiveBorrowService.getPage(page, size, applicantName,
                applicantEmployeeNumber);
        return Result.ok(pageResult);
    }

    @ApiOperation("获取档案借阅详情")
    @GetMapping("/{id}")
    public Result<ArchiveBorrow> getById(@ApiParam("ID") @PathVariable Long id) {
        return Result.ok(archiveBorrowService.getById(id));
    }

    @ApiOperation("新增或更新档案借阅记录")
    @PostMapping("/save")
    public Result<Boolean> saveOrUpdate(@RequestBody @Validated ArchiveBorrow archiveBorrow) {
        return Result.ok(archiveBorrowService.saveOrUpdate(archiveBorrow));
    }

    @ApiOperation("删除档案借阅记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> removeById(@ApiParam("ID") @PathVariable Long id) {
        return Result.ok(archiveBorrowService.removeById(id));
    }

    @ApiOperation("批量删除档案借阅记录")
    @DeleteMapping("/batch")
    public Result<Boolean> removeBatchByIds(@ApiParam("ID列表") @RequestBody Long[] ids) {
        return Result.ok(archiveBorrowService.removeByIds(java.util.Arrays.asList(ids)));
    }
}